using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace 域名端口扫描系统
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
            Control.CheckForIllegalCrossThreadCalls = false;
        }
        public Thread Scan;
        private CancellationTokenSource cts;
        private int runningThreadsCount;
        // 互斥锁 保障线程安全
        private readonly object consoleLock = new object();
        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("网站域名不能为空！");
            }
            else
            {
                if(checkBox1.Checked == false)
                {
                    //dataGridView1.Rows.Clear();
                    //cts = new CancellationTokenSource();
                    //runningThreadsCount = 0;
                    //int thre = Convert.ToInt32(textBox3.Text);
                    //for (int i = 0; i < thre; i++)
                    //{
                    //    ThreadPool.QueueUserWorkItem(DomaScan, cts.Token);
                    //    runningThreadsCount++;
                    //}
                    dataGridView1.Rows.Clear();
                    Scan = new Thread(new ThreadStart(DomaScan));
                    Scan.Start();
                }
                else
                {
                    //dataGridView1.Rows.Clear();
                    //cts = new CancellationTokenSource();
                    //runningThreadsCount = 0;
                    //int thre = Convert.ToInt32(textBox3.Text);
                    //for (int i = 0; i < thre; i++)
                    //{
                    //    ThreadPool.QueueUserWorkItem(DomaScanAll, cts.Token);
                    //    runningThreadsCount++;
                    //}
                    dataGridView1.Rows.Clear();
                    Scan = new Thread(new ThreadStart(DomaScanAll));
                    Scan.Start();
                }
            }
        }
        public void DomaScanAll()
        {
            button1.Enabled = false;
            button2.Enabled = true;
            toolStripStatusLabel2.Text = "扫描中";
            string url = textBox1.Text;
            for (int i = 0; i <= 65535; i++)
            {
                try
                {
                    string urlcode = "http://" + url + ":" + i;
                    toolStripStatusLabel4.Text = urlcode;
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlcode);
                    request.Method = "GET";
                    request.Timeout = 500;
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        StreamReader red = new StreamReader(response.GetResponseStream());
                        string html = red.ReadToEnd();//源码
                        Match m1 = Regex.Match(html, "<title>(.*)</title>");
                        string title = m1.Groups[1].Value;//标题
                        dataGridView1.Rows.Add(urlcode, html.Length, title, "200", DateTime.Now.ToString());
                    }
                    if (response.StatusCode == HttpStatusCode.Found)
                    {
                        StreamReader red = new StreamReader(response.GetResponseStream());
                        string html = red.ReadToEnd();//源码
                        Match m1 = Regex.Match(html, "<title>(.*)</title>");
                        string title = m1.Groups[1].Value;//标题
                        dataGridView1.Rows.Add(urlcode, html.Length, title, "302", DateTime.Now.ToString());
                    }
                    if (response.StatusCode == HttpStatusCode.InternalServerError)
                    {
                        StreamReader red = new StreamReader(response.GetResponseStream());
                        string html = red.ReadToEnd();//源码
                        Match m1 = Regex.Match(html, "<title>(.*)</title>");
                        string title = m1.Groups[1].Value;//标题
                        dataGridView1.Rows.Add(urlcode, html.Length, title, "500", DateTime.Now.ToString());
                    }

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
            button1.Enabled = true;
            button2.Enabled = false;
            toolStripStatusLabel2.Text = "线程已终止";
        }
        public void DomaScan()
        {
            button1.Enabled = false;
            button2.Enabled = true;
            toolStripStatusLabel2.Text = "扫描中";
            string url = textBox1.Text;
            string basecode = textBox2.Text;
            string[] code = basecode.Split(',');
            foreach (string urlcodes in code)
            {
                try
                {
                    string urlcode = "http://" + url + ":" + urlcodes;
                    toolStripStatusLabel4.Text = urlcode;
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlcode);
                    request.Method = "GET";
                    request.Timeout = 500;
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        StreamReader red = new StreamReader(response.GetResponseStream());
                        string html = red.ReadToEnd();//源码
                        Match m1 = Regex.Match(html, "<title>(.*)</title>");
                        string title = m1.Groups[1].Value;//标题
                        dataGridView1.Rows.Add(urlcode, html.Length, title, "200", DateTime.Now.ToString());
                    }
                    if (response.StatusCode == HttpStatusCode.Found)
                    {
                        StreamReader red = new StreamReader(response.GetResponseStream());
                        string html = red.ReadToEnd();//源码
                        Match m1 = Regex.Match(html, "<title>(.*)</title>");
                        string title = m1.Groups[1].Value;//标题
                        dataGridView1.Rows.Add(urlcode, html.Length, title, "302", DateTime.Now.ToString());
                    }
                    if (response.StatusCode == HttpStatusCode.InternalServerError)
                    {
                        StreamReader red = new StreamReader(response.GetResponseStream());
                        string html = red.ReadToEnd();//源码
                        Match m1 = Regex.Match(html, "<title>(.*)</title>");
                        string title = m1.Groups[1].Value;//标题
                        dataGridView1.Rows.Add(urlcode, html.Length, title, "500", DateTime.Now.ToString());
                    }

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
            button1.Enabled = true;
            button2.Enabled = false;
            toolStripStatusLabel2.Text = "线程已终止";
        }
        private void button2_Click(object sender, EventArgs e)
        {
            //if (cts != null)
            //{
            //    cts.Cancel();
            //    cts = null;
            //    button1.Enabled = true;
            //    button2.Enabled = false;
            //    toolStripStatusLabel4.Text = "0";
            //    toolStripStatusLabel2.Text = "扫描暂停";
            //}
            if (Scan != null && Scan.IsAlive)
            {
                button1.Enabled = true;
                button2.Enabled = false;
                toolStripStatusLabel4.Text = "0";
                toolStripStatusLabel2.Text = "扫描暂停";
                Scan.Abort();
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBox1.Checked == true)
            {
                textBox2.Enabled = false;
            }
            else
            {
                textBox2.Enabled = true;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if(checkBox1.Checked == true)
            {
                textBox2.Enabled = false;
            }
            else
            {
                textBox2.Enabled = true;
            }
        }
    }
}
